package com.ztorn.fiscale.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ztorn.common.core.web.controller.BaseController;
import com.ztorn.common.core.web.domain.AjaxResult;
import com.ztorn.common.log.annotation.Log;
import com.ztorn.common.log.enums.BusinessType;
import com.ztorn.common.security.annotation.RequiresPermissions;
import com.ztorn.fiscale.domain.FiscalePlatformConfig;
import com.ztorn.fiscale.domain.FiscalePlatformType;
import com.ztorn.fiscale.service.IFiscalePlatformConfigService;
import com.ztorn.fiscale.service.IFiscalePlatformTypeService;
import com.ztorn.common.core.utils.poi.ExcelUtil;
import com.ztorn.common.core.web.page.TableDataInfo;

/**
 * 平台授权配置Controller
 *
 * @author ztorn
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/platform")
public class FiscalePlatformController extends BaseController
{
    @Autowired
    private IFiscalePlatformConfigService fiscalePlatformConfigService;

    @Autowired
    private IFiscalePlatformTypeService fiscalePlatformTypeService;

    /**
     * 查询平台授权配置列表
     */
    @RequiresPermissions("fiscale:platform:list")
    @GetMapping("/list")
    public TableDataInfo list(FiscalePlatformConfig fiscalePlatformConfig)
    {
        startPage();
        List<FiscalePlatformConfig> list = fiscalePlatformConfigService.selectFiscalePlatformConfigList(fiscalePlatformConfig);
        return getDataTable(list);
    }

    /**
     * 查询平台类型列表
     */
    @RequiresPermissions("fiscale:platform:query")
    @GetMapping("/types")
    public AjaxResult getTypes()
    {
        List<FiscalePlatformType> list = fiscalePlatformTypeService.selectActivePlatformTypes();
        return success(list);
    }

    /**
     * 根据平台类型获取配置字段
     */
    @RequiresPermissions("fiscale:platform:query")
    @GetMapping("/configFields/{typeCode}")
    public AjaxResult getConfigFields(@PathVariable("typeCode") String typeCode)
    {
        FiscalePlatformType platformType = fiscalePlatformTypeService.selectFiscalePlatformTypeByTypeCode(typeCode);
        if (platformType != null)
        {
            return success(platformType.getConfigFields());
        }
        return error("平台类型不存在");
    }

    /**
     * 导出平台授权配置列表
     */
    @RequiresPermissions("fiscale:platform:export")
    @Log(title = "平台授权配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FiscalePlatformConfig fiscalePlatformConfig)
    {
        List<FiscalePlatformConfig> list = fiscalePlatformConfigService.selectFiscalePlatformConfigList(fiscalePlatformConfig);
        ExcelUtil<FiscalePlatformConfig> util = new ExcelUtil<FiscalePlatformConfig>(FiscalePlatformConfig.class);
        util.exportExcel(response, list, "平台授权配置数据");
    }

    /**
     * 获取平台授权配置详细信息
     */
    @RequiresPermissions("fiscale:platform:query")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable("configId") Long configId)
    {
        return success(fiscalePlatformConfigService.selectFiscalePlatformConfigByConfigId(configId));
    }

    /**
     * 新增平台授权配置
     */
    @RequiresPermissions("fiscale:platform:add")
    @Log(title = "平台授权配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FiscalePlatformConfig fiscalePlatformConfig)
    {
        return toAjax(fiscalePlatformConfigService.insertFiscalePlatformConfig(fiscalePlatformConfig));
    }

    /**
     * 修改平台授权配置
     */
    @RequiresPermissions("fiscale:platform:edit")
    @Log(title = "平台授权配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FiscalePlatformConfig fiscalePlatformConfig)
    {
        return toAjax(fiscalePlatformConfigService.updateFiscalePlatformConfig(fiscalePlatformConfig));
    }

    /**
     * 删除平台授权配置
     */
    @RequiresPermissions("fiscale:platform:remove")
    @Log(title = "平台授权配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable Long[] configIds)
    {
        return toAjax(fiscalePlatformConfigService.deleteFiscalePlatformConfigByConfigIds(configIds));
    }

    /**
     * 测试平台连接
     */
    @RequiresPermissions("fiscale:platform:test")
    @Log(title = "测试平台连接", businessType = BusinessType.OTHER)
    @PostMapping("/test")
    public AjaxResult testConnection(@RequestBody FiscalePlatformConfig fiscalePlatformConfig)
    {
        try
        {
            String result = fiscalePlatformConfigService.testPlatformConnection(fiscalePlatformConfig);
            return success(result);
        }
        catch (Exception e)
        {
            return error("连接测试失败：" + e.getMessage());
        }
    }

    /**
     * 同步平台数据
     */
    @RequiresPermissions("fiscale:platform:sync")
    @Log(title = "同步平台数据", businessType = BusinessType.OTHER)
    @PostMapping("/sync/{configId}")
    public AjaxResult syncData(@PathVariable("configId") Long configId)
    {
        try
        {
            String result = fiscalePlatformConfigService.syncPlatformData(configId);
            return success(result);
        }
        catch (Exception e)
        {
            return error("同步失败：" + e.getMessage());
        }
    }
}
